package com.stu.tools.easyexcle.write;

import com.alibaba.excel.EasyExcel;
import com.stu.tools.MyTools;
import com.stu.vo.ExcelData;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 写的常见写法
 *
 * @author Jiaju Zhuang
 */
@Component
public class WriteTest {


    MyTools t = new MyTools();
//    @Autowired
//    private MajorService majorService;

    public void simpleWrite() {
        // 写法1
        String rootPath = t.getPath("ExcelMod/");
        String fileName = rootPath + "studentsMod.xlsx";
        // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
        EasyExcel.write(fileName, ExcelData.class).sheet("模板").doWrite(data());

    }

    public List<ExcelData> data() {

        Date d = new Date();
        List<ExcelData> list = new ArrayList<>();
        for (int i = 1; i <= 1; i++) {
            ExcelData s = new ExcelData();
            s.setName("学生" + i);
            s.setID(dateRandom18());
            s.setRegisterTime(d);
            s.setState(MyTools.getStatus(getNum3()));
            s.setStuId(i);
            s.setClassName("班级" + getNum(5));
            int j = getNum(4);
            s.setCollegeName("学院" + j);
//            String str = majorService.majorInfo(getNum(20), j).getMajorName();
            s.setMajorName("专业" + getNum(5));
            list.add(s);
        }
        return list;
    }

    public static String dateRandom18() {
        //获取当前年月日
        SimpleDateFormat yyyyMMdd = new SimpleDateFormat("YYYYMMDD");
        String date = yyyyMMdd.format(new Date());
        //获取时间戳后6位
        String timeMillis = String.valueOf(System.currentTimeMillis());
        String fiveNumber = timeMillis.substring(timeMillis.length() - 6);
        //获取随机数后4位
        String tempRandom = String.valueOf(Math.random());
        String number = tempRandom.substring(tempRandom.length() - 3);
        //生成18位随机时间戳数字
        return number + date + fiveNumber;
    }

    public static int getNum(int i) {
        return (int) (Math.random() * i + 1);
    }

    public static int getNum3() {
        return (int) (Math.random() * 3);
    }
}
